<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2021-07-05 10:16:09
 * @LastEditors: Sam
 * @LastEditTime: 2023-01-05 18:09:20
 */


namespace app\admin\controller;

// 菜单管理
use think\App;
use think\facade\Db;
use think\Env;
use think\Request;
use app\common\support\Generate;
use app\common\BaseController;
use hg\apidoc\annotation as Apidoc;


/**
 * @Apidoc\Title("自动生成")
 */
class Curd extends BaseController{

	private $modu;
    private $name;
    private $dir;
    private $namespaceSuffix;
    private $nameLower;
    private $data;

        /**
     * @Apidoc\Title("列表")
     * @Apidoc\Url("admin/curd_index")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("data", type="array", desc="业务数据",replaceGlobal="true",
     *   @Apidoc\Returned("start",type="int",desc="起始位置"),
     *   @Apidoc\Returned("limit",type="int",desc="查询数量"),
     *   @Apidoc\Returned("data",type="array",desc="返回数据"),
     *   @Apidoc\Returned("total",type="array",desc="数据总量")
     * )
     */
	public function index(){
        $dbtables = Db::query("SELECT TABLE_NAME Name FROM information_schema.tables WHERE TABLE_NAME LIKE '%".config('database.connections.mysql.prefix')."_%'");
        if(request()->param('table')){
            $table      = request()->param('table');
            $prefix     = app()->env->get(SYSTEM.'db_pre');
            $tableInfo  = Db::table($table)->getFieldsType();
            foreach ($tableInfo as $key => $value) {
                $tableInfo[] = $key;
                unset($tableInfo[$key]);
            }
            $controller = parseName(preg_replace('/^(' . $prefix . ')/', '', $table), 1);
           return  $this->success('ok',[
                'table_info' => ($tableInfo),
                'controller' => $controller,
                'tables'      => $dbtables,
            ]);
        }else{
           return  $this->success('ok',['table_info'=>json_encode([]),'tables'=>$dbtables]);
        }
    }



        /**
     * @Apidoc\Title("提交")
     * @Apidoc\Url("/admin/curd_run")
     * @Apidoc\Method("post")
     * @Apidoc\Returned("data", type="array", desc="业务数据",replaceGlobal="true",
     *   @Apidoc\Returned("start",type="int",desc="起始位置"),
     *   @Apidoc\Returned("limit",type="int",desc="查询数量"),
     *   @Apidoc\Returned("data",type="array",desc="返回数据"),
     *   @Apidoc\Returned("total",type="array",desc="数据总量")
     * )
     */
    public function run(){
        $generate = new \app\common\support\Generate;
        $data     = request()->post();
         try{
            $result = $generate->run($data, request()->post('file'));
         }catch(\Exception $e){
            return $this->error($e->getMessage().$e->getFile().$e->getLine());
         }
         return $this->success('创建成功',$result);
    }
}